package com.example.androidframework.ui.course.seriesback

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.androidframework.R
import com.martin.lib_base.ktx.logw
import com.martin.lib_base.utils.FishActivityCollector
import com.martin.lib_base.utils.FishStackManager

class PageDActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_page_d)
        FishStackManager.pushActivity(this)
        FishActivityCollector.addActivity(this)

        findViewById<Button>(R.id.button_a).setOnClickListener {
            popToActivityA()
        }

        findViewById<Button>(R.id.button_root).setOnClickListener {
            popAllActivity()
        }

        findViewById<Button>(R.id.button_flags).setOnClickListener {
            pushFistActivityWithClearTop()
        }

        findViewById<Button>(R.id.button_collector).setOnClickListener {
            popToActivityAWithActivityCollector()
        }
    }

    // 返回PageA
    private fun popToActivityA() {
        FishStackManager.popTopActivities(PageAActivity::class.java)
    }

    // 返回根目录
    private fun popAllActivity() {
        FishStackManager.popAllActivities()
    }

    // 返回PageA
    private fun popToActivityAWithActivityCollector() {
        FishActivityCollector.popTopActivities(PageAActivity::class.java)
    }

    // 返回第一个页面
    // 动效还是push的动效, 效果不好
    // 页面会重新创建,此方式不合适
    private fun pushFistActivityWithClearTop() {
        val intent = Intent(this, PageAActivity::class.java)
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        FishStackManager.popActivity(this)
        FishActivityCollector.removeActivity(this)
        logw("Page D on destory")
    }

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, PageDActivity::class.java)
            context.startActivity(intent)
        }
    }
}